package cn.cloudplug.friendsrecommend;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.entity.Member;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;

public class AddTeam2Activity extends Activity {
	private SwipeMenuListView lv_swip;
	private ImageView iv_home;
	private List<Member> members;
	private MyMemberAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_team2);
		getTestData();
		setView();
	}

	private void getTestData() {
		// TODO Auto-generated method stub
		members = new ArrayList<Member>();
		for (int i = 0; i < 6; i++) {
			Member m = new Member(i, i, "张" + i + "山", (int) (Math.random() * 10 * i) + "", "2015年4月1日");
			members.add(m);
		}
	}

	private void setView() {
		// TODO Auto-generated method stub
		iv_home = (ImageView) findViewById(R.id.iv_home);
		iv_home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(AddTeam2Activity.this, MainActivity.class ));
			}
		});
		lv_swip = (SwipeMenuListView) findViewById(R.id.lv_swip);

		adapter = new MyMemberAdapter();

		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				/*
				 * // create "open" item SwipeMenuItem openItem = new
				 * SwipeMenuItem( getApplicationContext()); // set item
				 * background openItem.setBackground(new
				 * ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE))); // set item
				 * width openItem.setWidth(dp2px(90)); // set item title
				 * openItem.setTitle("Open"); // set item title fontsize
				 * openItem.setTitleSize(18); // set item title font color
				 * openItem.setTitleColor(Color.WHITE); // add to menu
				 * menu.addMenuItem(openItem);
				 */
				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
				// set item width
				deleteItem.setWidth(dp2px(90));
				// set a icon
				deleteItem.setIcon(android.R.drawable.ic_delete);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		lv_swip.setMenuCreator(creator);

		lv_swip.setAdapter(adapter);
		
		// step 2. listener item click event
		lv_swip.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(int position, SwipeMenu menu,
					int index) {
				switch (index) {
				case 0:
					members.remove(position);
					adapter.notifyDataSetChanged();
					break;
				}
				return false;
			}
		});

		// set SwipeListener
		lv_swip.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				// swipe start
			}

			@Override
			public void onSwipeEnd(int position) {
				// swipe end
			}
		});

		lv_swip.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(AddTeam2Activity.this,position + " long click", 0).show();
				return false;
			}
		});
		
		
		
	}
	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}
	
	
	class MyMemberAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return members.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return members.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder vh=null;
			if (convertView == null) {
				convertView = View.inflate(getApplicationContext(),
						R.layout.list_item_row, null);
				vh=new ViewHolder();
				vh.iv_photo = (ImageView) convertView.findViewById(R.id.iv_photo);
				vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(vh);
			}
			vh= (ViewHolder) convertView.getTag();
			final Member m = members.get(position);
			vh.iv_photo.setImageResource(R.drawable.ic_launcher);
			vh.tv_name.setText(m.getName());
			return convertView;
		}

		class ViewHolder {
			ImageView iv_photo;
			TextView tv_name;
		}

	}
}
